﻿using System;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class VehicleEdit : System.Web.UI.Page
    {
        protected ReqVehicleMains reqVehicleMains;
        protected string empName;
        private ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string formNo = Request.QueryString["formNo"];
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<ReqVehicleMains>(r => r.ReqVehicleSubs);
                ctx.LoadOptions = dlo;
                reqVehicleMains = ctx.ReqVehicleMains.Single(r => r.FormNo == formNo);

                FormNo.Value = reqVehicleMains.FormNo;
                Vehicle.Value = reqVehicleMains.Vehicle;
                Riders.Value = reqVehicleMains.Riders;
                RiderCnt.Value = reqVehicleMains.RiderCnt.ToString();
                PreStartTime.Value = reqVehicleMains.PreStartTime.ToString("yyyy-MM-dd HH:mm:ss");
                PreEndTime.Value = reqVehicleMains.PreEndTime.ToString("yyyy-MM-dd HH:mm:ss");
                Cause.Value = reqVehicleMains.Cause;

                rptList.DataSource = reqVehicleMains.ReqVehicleSubs;
                rptList.DataBind();
                Employee employee = ctx.Employees.Single(r => r.Id == reqVehicleMains.EmpId);
                empName = employee.Name;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            _formSave(0);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            _formSave(1);
        }
        private void _formSave(int isSubmit)
        {
            reqVehicleMains = ctx.ReqVehicleMains.Single(r => r.FormNo == FormNo.Value);
            reqVehicleMains.ReqDate = DateTime.Now;
            reqVehicleMains.Riders = Riders.Value;
            reqVehicleMains.RiderCnt = int.Parse(RiderCnt.Value);
            reqVehicleMains.Vehicle = Vehicle.Value;
            reqVehicleMains.PreStartTime = DateTime.Parse(PreStartTime.Value);
            reqVehicleMains.PreEndTime = DateTime.Parse(PreEndTime.Value);
            reqVehicleMains.Cause = Cause.Value;
            reqVehicleMains.StatusId = isSubmit;

            //更新时删除之前的签核数据
            var list = from r in ctx.SignFlows where r.FormNo == FormNo.Value select r;
            ctx.SignFlows.DeleteAllOnSubmit(list);

            ctx.SubmitChanges();
            if (isSubmit == 1)//写签核流程
            {
                FlowSet fs = new FlowSet();
                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                fs.SetSignFlow(FormNo.Value, ebi);
            }
            Response.Write("<script>alert('修改成功');window.close();</script>");
        }
    }
}